<template>
	<view>
		<!-- 主体 -->
		<view class="page write">
			<!-- 头部 -->
			<view class="write_head flex-row-center">
				<view class="code_box" @click="OnscanCode">
					<image src="https://static.newbeaconhotels.com/app/hotel/my/scancode.png" mode=""></image>
					<view>扫码核销</view>
				</view>
				<view class="code_border"></view>
				<view class="code_box" @click="OnComPopupshow">
					<image src="https://static.newbeaconhotels.com/app/hotel/my/scancodea.png"></image>
					<view>兑换码核销</view>
				</view>
			</view>
			<!-- 搜索 -->
			<view class="search flex-row-between">
				<view class="search_box flex-row-start">
					<input class="input_search" placeholder="请输入券名" maxlength="50" placeholder-class="placeholderClass"
						confirm-type="search" @input="Oninput" :value="initData.param.name" />
					<view class="search_img flex-row-center" @click="Onquery">
						<image src="https://static.newbeaconhotels.com/app/hotel/my/search.png"></image>
					</view>
				</view>
				<view class="font-323232 font14">核销记录 {{infoobj.count?infoobj.count:0}} 条</view>
			</view>
			<!-- 数据 -->
			<LoadMore ref="LoadMore" id="LoadMore" :diffHeight="120" :initData="initData" @dataRefresh='dataRefresh' @infoRefresh="infoRefresh">
				<view class="box-shadow write_bos" v-for="(item,index) in writeList" :key="index">
					<view class="write_box">
						<view class="font-323232 font14 font-b">{{item.coupon.name}}</view>
						<view class="write_discount">
							<view class="font15" v-if="item.coupon.coupon_type==10">
								满{{item.coupon.full_money}}减{{item.coupon.reduce_money}}</view>
							<view class="font15" v-if="item.coupon.coupon_type==20">订单{{item.coupon.discount*10}}折(不包含邮费)
							</view>
							<view class="font15" v-if="item.coupon.coupon_type==30">立减{{item.coupon.reduce_money}}(无门槛限制)
							</view>
							<view class="font15" v-if="item.coupon.coupon_type==40">兑换商品(无门槛限制)</view>
							<view class="font15" v-if="item.coupon.coupon_type==50">停车券(无门槛限制)</view>
						</view>
						<view class="write_volume flex-row-start">
							<view>{{item.coupon.coupon_type_name}}</view>
							<text class="font-969696 font12">有效期：{{item.coupon.begin_time}}至{{item.coupon.end_time}}</text>
						</view>
						<view class="name_box flex-row-between">
							<view class="name_boxa flex-row-start">
								<image mode="aspectFill" :src="item.user_info.avatar"></image>
								<view class="font-969696 font13">{{item.user_info.name}}</view>
							</view>
							<view class="font-969696 font13">券码:{{item.coupon_code}}</view>
						</view>
					</view>
					<view class="write_time flex-row-between font13">
						<view>核销时间:{{item.verify_time}}</view>
						<view>核销人:{{item.verify_user_name}}</view>
					</view>
					<image class="write_img" src="https://static.newbeaconhotels.com/app/D2103-business-js/my/write.png"></image>
				</view>
			</LoadMore>
		</view>
		<!-- 弹窗 -->
		<uni-popup ref="popup" type="bottom">
			<view class="ComPopup_bos">
				<input class="input_box" placeholder="请输入核销券码" maxlength="20" :value="code"
					placeholder-class="placeholderClass" @input="Oncode" />
				<!-- 提交 -->
				<view class="custom_class flex-row-center" @click="Oninfo">查看验证</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import LoadMore from "@/components/j-loadMore/index.vue";
	export default {
		components: {
			LoadMore
		},
		data() {
			return {
				h5share: true,
				code: '', //要核销得码
				wxSdk: false, //H5api
				infoobj: {}, //返回对象
				writeList: [],
				//传参数据
				initData: {
					url: 'storeVerifyCouponList', //接口
					//data里得传参
					param: {
						name: ''
					},
				},
			}
		},
		onLoad(options) {
			// #ifdef H5
			this.$wxapi.OnwxSdk().then(res => {
				if (res.code == 1) {
					this.Onshare()
				}
				this.wxSdk = res.code == 1 ? true : false
			})
			// #endif
		},
		methods: {
			//扫码核销
			OnscanCode() {
				// #ifdef MP-WEIXIN
				wx.scanCode({
					success: (res) => {
						uni.setStorage({
							key: 'firstType',
							data: true,
						})
						this.$Router.push('/' + res.path)
					},
					fail: (res) => {
						uni.showToast({
							icon: 'none',
							duration: 2000,
							title: '扫码解析失败'
						});
					}
				})
				// #endif
				// #ifdef H5
				if (this.wxSdk) {
					this.$wxapi.Oncode().then(res => {
						if (res.code == 1) {
							uni.showModal({
								title: '开发测试',
								confirmText: '去核销',
								confirmColor: '#03004c',
								cancelText: '取消',
								content: res.msg,
								success: function(res1) {
									if (res1.confirm) {
										uni.setStorage({
											key: 'firstType',
											data: true,
										})
										window.location.href = res.msg
									}
								}
							});
						} else {
							uni.showToast({
								icon: 'none',
								duration: 2000,
								title: '扫码解析失败'
							});
						}
					})
				} else {
					uni.showToast({
						icon: 'none',
						duration: 2000,
						title: '唤起摄像头失败'
					});
				}
				// #endif
			},
			//接口数据返回
			dataRefresh(e) {
				e.forEach(item => {
					item.coupon.begin_time = this.$base.formatTime(new Date(item.coupon.begin_time * 1000),
						"yyyy/MM/dd")
					item.coupon.end_time = this.$base.formatTime(new Date(item.coupon.end_time * 1000),
						"yyyy/MM/dd")
				})
				this.writeList = e
			},
			//返回
			infoRefresh(e) {
				this.infoobj = e
			},
			//筛选
			Oninput(e) {
				this.initData.param.name = e.detail.value
			},
			Onquery() {
				this.$refs.LoadMore.oncutRefresh(this.initData)
			},
			//弹窗
			OnComPopupshow() {
				this.$refs.popup.open()
			},
			Oncode(e) {
				this.code = e.detail.value
			},
			//去核销
			Oninfo() {
				if (this.code == '') {
					uni.showToast({
						icon: 'none',
						duration: 2000,
						title: '请输入核销码'
					});
					return
				}
				// #ifdef MP-WEIXIN
				this.$Router.push('/pages/coupon/info?data=id.' + this.code + ',se.hx')
				// #endif
				// #ifdef H5
				uni.setStorage({
					key: 'firstType',
					data: true,
				})
				window.location.href = this.$Config.http_h5_url + '/pages/coupon/info?data=id.' + this.code + ',se.hx'
				// #endif
				this.code = ''
				this.$refs.popup.close()
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
